//
//  STSpreadOrderDetailViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/24.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STSpreadOrderDetailViewController.h"

#import "PayViewController.h"

@interface STSpreadOrderDetailViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) STSpreadOrderDetailViewModel* viewModel;

// UI
//@property (weak, nonatomic) UILabel *addressLabel;

//IBOutlet
@property (weak, nonatomic) IBOutlet UILabel* payTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel* citysLabel;
@property (weak, nonatomic) IBOutlet UILabel* dateLabel;
@property (weak, nonatomic) IBOutlet UILabel* totalPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel* discount4CoinLabel;
@property (weak, nonatomic) IBOutlet UILabel* discountLabel;
@property (weak, nonatomic) IBOutlet UILabel* discountPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel* finalPayLabel;
@property (weak, nonatomic) IBOutlet UILabel* orderNumLabel;
@property (weak, nonatomic) IBOutlet UILabel* orderPayTimeLabel;
@property (weak, nonatomic) IBOutlet UIButton* payBtn;

@end

@implementation STSpreadOrderDetailViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.hidesBottomBarWhenPushed = YES;
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x) {
        @strongify(self)
        
    }];
}

- (void)initUI
{
    
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [[willAppearSignal take:1] subscribeNext:^(id x)
    {
        @strongify(self)
        
        [self.viewModel.reqDetailCmd execute:nil];
    }];
    
    [didAppearSignal subscribeNext:^(id x)
     {
         @strongify(self)
         
         // ScrollView 内容 未能在此时才约束
         
         //Request
     }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [[[[viewDidLayoutSVSignal take:1] zipWith:didAppearSignal] deliverOnMainThread] subscribeNext:^(id x) {
        @strongify(self)
        
        //重置 更新 frame
        [self _setupViews];
    }];
    
    self.payBtn.rac_command = self.viewModel.payCmd;
    [[self.viewModel.payCmd.executionSignals.switchToLatest deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self);
         
         [self _push2PayVC:x];
     }];
}

- (void)_setupViews
{
    @weakify(self);
    
    [[RACObserve(self.viewModel, isHavePay) distinctUntilChanged] subscribeNext:^(id x)
    {
        @strongify(self);
        
        BOOL isHavePay = [x boolValue];
        
        for (NSLayoutConstraint* curConstraint in self.payBtn.constraints)
        {
            if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
            {
                curConstraint.constant = isHavePay ? 44.0f : 0.0f;
            }
            else if (curConstraint.firstAttribute == NSLayoutAttributeTop || curConstraint.firstAttribute == NSLayoutAttributeBottom)
            {
                curConstraint.constant = isHavePay ? 12.0f : 0.0f;
            }
        }
    }];
    
    RAC(self.payTypeLabel, text) = RACObserve(self.viewModel, payTypeLabel);
    RAC(self.citysLabel, text) = RACObserve(self.viewModel, citysLabel);
    RAC(self.dateLabel, text) = RACObserve(self.viewModel, dateLabel);
    RAC(self.totalPriceLabel, text) = RACObserve(self.viewModel, totalPriceLabel);
    RAC(self.discount4CoinLabel, text) = RACObserve(self.viewModel, discount4CoinLabel);
    RAC(self.discountLabel, text) = RACObserve(self.viewModel, discountLabel);
    RAC(self.discountPriceLabel, text) = RACObserve(self.viewModel, discountPriceLabel);
    RAC(self.finalPayLabel, text) = RACObserve(self.viewModel, finalPayLabel);
    RAC(self.orderNumLabel, text) = RACObserve(self.viewModel, orderNumLabel);
    RAC(self.orderPayTimeLabel, text) = RACObserve(self.viewModel, orderPayTimeLabel);
    
    [self.payBtn addTarget:self action:@selector(_onBtnPay) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -- sub class can override it

#pragma mark - 监听
- (void)_onBtnPay
{
    
}

#pragma mark - 工具

#pragma mark - 跳转
- (void)_push2PayVC:(NSDictionary*)orderInfo
{
    NSString* money = [NSString decimalwithFormat:@"0.00" floatV:[orderInfo[@"pay_price"] floatValue]];
    
    PayViewModel* vm = [[PayViewModel alloc] initWithParams:@{SUViewModelTitleKey:@"支付"}];
    vm.pay4Type = 1;
    vm.orderID = orderInfo[@"spread_id"];
    vm.orderSN = orderInfo[@"spread_sn"];
    vm.money = money;
    PayViewController* vc = [[PayViewController alloc] initWithViewModel:vm];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
